***** LINUX TERMINAL *****

*** INTRODUCTION ***

The terminal is Linux's version of Windows' command-line prompt, on steroids with full internet capabilities.
If you've ever used DOS, then you'll have a big headstart on learning the terminal.

There are essentially 2 types of terminal you will use.  The first is ran from your desktop (GUI - graphical
user interface) as an application.  This type of terminal is a "terminal emulator".  The terminology isn't
important, but can be useful to know as your skill progresses.

The 2nd type of terminal is always running simultaneous to your desktop.  If your desktop freezes (can't move
mouse or do anything), you can switch to one of these terminals to list your running processes and kill the
troublemaker.  These terminals are called "virtual terminals".  You can switch to virtual terminal 1, known
as tty1, by pressing Ctrl+Alt+F1.  DON'T do that until you read the next line, how to get back to your desktop.
Your desktop runs on tty7, and you can get back by pressing Ctrl+Alt+F7.

To get to the terminal, go to Applications, Accessories, Terminal.  
When you run it as a normal user, it starts you in your own home directory.

*** IMPORTANT INFORMATION ***

The linux terminal is a very powerful thing.  You can issue a command to do something such as delete your 
entire hard drive, and if given administrative rights, it will do exactly as you command it, regardless of
any negative outcome.  Be cautious using commands that you do not understand, especially if they're being 
run as administrator.

By default, commands you type are executed with normal user rights.  To run a command with administrator 
rights, prefix it with "sudo".  The SU stands for Super User, super user do this, super user do that.
To gain super user rights, you'll be prompted for your admin password, which is the same password you use
to login with.  The password entry is invisible, just type your password and hit enter.

*** BASIC FILESYSTEM INTRO ***

The "root" of your hard drive filesystem is the starting point.  In Windows the root path is C:\
In linux it's simply a forward slash /.  The path to your home directory is /home/yourusername/

Your home directory (/home/yourusername/) is abbreviated in the terminal as a tilde ~
The tilde is located above your tab key, to the left of the 1 key, hold shift.

Your documents folder is /home/yourusername/Documents/  Music is /home/yourusername/Music
Abbreviated they are ~/Documents and ~/Music

File and folder names are CASE sensitive.  ~/Documents is not the same as ~/documents
The Ubuntu terminal has an auto-complete feature turned on.  This will auto-complete
commands, filenames, and foldernames.  The TAB key triggers the auto-complete function.

  ex : ~/Do <hit tab key> to auto-complete as ~/Documents

The terminal uses a pattern matching system called regex (regular expressions).  Regex is extremely powerful
and can give a power user tons of abilities.  For you as a new user, all you need to know is that certain
characters in a filename or foldername need to be "escaped", which simply means to put a backslash \ in front
of the character.  The most common one is a space.  

  ex : ~/Videos/Music\ Videos/

Also, a splat * (I call it a splat, you call it what you want), means to match everything.  ~/Downloads/*
would match everything in the Downloads folder.

*** BASIC COMMANDS ***

List files in current directory : ls

List files and show detailed info : ls -n

Change directory : cd <directory_name_or_path>

  ex : cd Documents			(if current directory is your home directory ~)
  ex : cd ~/Documents			(if not currently in home directory ~)
  ex : cd Videos/Music\ Videos/		(if current directory is your home directory ~)
  ex : cd ~/Videos/Music\ Videos/	(if not currently in home directory ~)
  ex : cd ~/Videos/Mu <hit tab key>	(auto-complete to Music Videos directory)

Go back one directory : cd ..

  If typed in ~/Videos/Music\ Videos/, it would take you to ~/Videos
  If typed in ~/Videos, it would take you to ~

Make a directory : mkdir <directory_name>

  ex : mkdir Music\ Videos		(if current directory is ~/Videos)
  ex : mkdir ~/Videos/Music\ Videos	(if not currently in ~/Videos)

Remove a directory : rmdir <directory_name>

  ex : rmdir ~/Videos/Clips		(typed from anywhere)
  ex : rmdir Clips			(if current directory is ~/Videos)

  Note : Directory must be empty to be removed

Copy a file to another directory : cp <file_name> <destination_path>

  ex : cp FunnyClown.mpg ~/Videos/Clips
  ex : cp FunnyClown.mpg Clips		(if current directory is ~/Videos)
  ex : cp mydocument.doc ~/Documents

  The -v option means "verbose", which in this case means to show the progress as each file is copied

  ex : cp ~/Downloads/*.txt ~/Documents/Plain\ Text\ Files/ -v 

Move a file to another directory : mv <file_name> <destination_path>

  Same as Copy above

*** DANGEROUS COMMANDS ***

Remove a file : rm <file_name>

  ex : rm FunnyClown.mpg 		(if current directory is ~/Videos/Clips)
  ex : rm ~/Videos/Clips/FunnyClown.mpg

A * (I call it a splat) matches everything.  

  ex : rm *  				(would remove everything in the current directory)
  ex : rm ~/Videos/Clips/*		(would remove everything in ~/Videos/Clips/)

The -r option on the rm command means recursive, which means to descend into subdirectories

  ex : rm -r ~/Videos/Clips		(would remove the entire Clips directory and any subdirectories and files inside it)
  ex : rm -r ~/Videos			(would remove your entire Videos directory)
  ex : rm -r ~				(would remove/delete your entire Home directory and everything inside it)

Pay close attention to the last 4 lines.  You would NEVER want to type "rm -r ~", that would delete all your personal data.
Be careful seeking instructions online or following commands given by others, there are assholes out there that will tell
you to type commands that would destroy your data... they're rare, but they do exist - make sure you understand what you're
doing before you type any commands in the terminal!

*** LISTING AND KILLING RUNNING PROCESSES ***

The command to list running processes is "top".  Top has a whole set of commands within itself, I'll list the common ones.

  >   change column to sort by (process name, process ID number, memory usage, cpu usage, etc.)
  <   change column (backwards)
  q   quit

So basically, if for instance firefox froze up so bad that you couldn't move your mouse anymore, you could type
Ctrl+Alt+F1 to go to Virtual Terminal 1 (tty1), which is the core behind your desktop.  Then type "top" to list running
processes, and hit > until it got to the CPU usage column, and firefox would be on the top of the list.  Find the process
you're looking for, then remember or write down the Process ID number that is used to identify it from for example another
instance of firefox running (that isn't frozen).

The command to kill a running process is "kill".  Kill requires and uses the Process ID number mentioned above.  Kill requires
use of one of two options, -3 and -9.  -3 means to ask the program nicely to close, same as clicking the "X" button to close a
program.  -9 means to literally kill the program, forcing it to quit whether it wants to or not.  Frozen programs will
surprisingly close sometimes when asked with -3.  When I go to kill a program, let's say for instance PID 1234, I will :

  kill -3 1234       
  kill -3 1234      (typed again... if the process is already dead it will return an error saying 'no such process')
  kill -9 1234      (only if the -3 failed and the 2nd round above didn't say 'no such process')

This really isn't as complex as it sounds.  This is one of the best things about the way Linux is designed based on this
command-line terminal stuff is that you can get yourself out of a freeze-up jam almost all the time by simply jumping to
one of the simultaneous virtual terminals, listing/sorting the processes, and then killing the troublemaker.  You can't
even dream of doing that in Windows when your desktop is frozen!